<script>
export default {
    props: {
        src: "",
        name: {
            type: String,
            default: ""
        }
    },
    render(createElement) {
        return "";
    },
    methods: {
        tryEmit() {
            if(window[this.name]) {
                this.$emit("on-loaded", window[this.name]);
            }else {
                var i = 0;
                var interval = setInterval(() => {
                    i += 100;
                    if(window[this.name]) {
                        this.$emit("on-loaded", window[this.name]);
                        clearInterval(interval);
                    }else if(i > 10000) {
                        console.error(`脚本${this.src}加载失败`);
                        clearInterval(interval);
                    }
                }, 100);
            }
        }
    },
    mounted() {
        var _doc = document.getElementsByTagName('head')[0]; 
        var js = document.createElement('script'); 
        js.setAttribute('type', 'text/javascript'); 
        js.setAttribute('src', this.src); 
        _doc.appendChild(js);
        
        if (!/*@cc_on!@*/0) { //if not IE 
            //Firefox2、Firefox3、Safari3.1+、Opera9.6+ support js.onload 
            js.onload = () => {
                this.tryEmit();
            } 
        } else { 
            //IE6、IE7 support js.onreadystatechange 
            js.onreadystatechange = () => { 
                if (js.readyState == 'loaded' || js.readyState == 'complete') {
                    this.tryEmit();
                } 
            } 
        } 
        return false; 
    }
}
</script>